
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.bo.IEmpSalaryAcctitemsBo;
import com.wyzz.hr.dao.IEmpSalaryAcctitemsDao;
import com.wyzz.hr.pojo.Empbenefitplan;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empsalaryacctitems;
import com.wyzz.hr.pojo.Empsalaryadj;
import com.wyzz.hr.pojo.Empsalaryconfig;
import com.wyzz.hr.pojo.Empsalarypay;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;
import org.springframework.util.ObjectUtils;


public class EmpSalaryAcctitemsBoImpl implements IEmpSalaryAcctitemsBo {

	public EmpSalaryAcctitemsBoImpl() {
	}

	public List getItemsByAcctversion(String acctversionId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Empsalaryacctitems.class);
		detachedCriteria.setFetchMode(Empsalaryacctitems.PROP_ESAI_ESDD,
				FetchMode.JOIN);
		System.out.println(acctversionId);
		detachedCriteria.add(Restrictions.eq("esaiEsav.id", acctversionId));
		detachedCriteria.addOrder(Order
				.asc(Empsalaryacctitems.PROP_ESAI_DATA_SEQ));
		System.out.println(empsalaryacctitemsDao.findByCriteria(detachedCriteria).size()+"ooooooo");
		return empsalaryacctitemsDao.findByCriteria(detachedCriteria);
	}

	public Map getItemsByAcctversion(List acctversionIds) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalaryacctitems.class);
		detachedCriteria.setFetchMode(Empsalaryacctitems.PROP_ESAI_ESDD,
				FetchMode.JOIN);
		detachedCriteria.add(Restrictions.in("esaiEsav.id", acctversionIds));
		detachedCriteria.addOrder(Order
				.asc(Empsalaryacctitems.PROP_ESAI_DATA_SEQ));
		List acctItems = empsalaryacctitemsDao.findByCriteria(detachedCriteria);
		Map cacheMap = new HashMap();
		String key;
		List tmpList;
		for (Iterator i$ = acctItems.iterator(); i$.hasNext(); cacheMap.put(
				key, tmpList)) {
			Empsalaryacctitems item = (Empsalaryacctitems) i$.next();
			key = item.getEsaiEsav().getId();
			tmpList = (List) cacheMap.get(key);
			if (tmpList == null)
				tmpList = new ArrayList();
			tmpList.add(item);
		}

		return cacheMap;
	}

	public List getAllItemsByEsavIds(List acctversionIds) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalaryacctitems.class);
		detachedCriteria.createAlias("esaiEsav", "esaiEsav", 1);
		detachedCriteria.createAlias("esaiEsav.esavEsac", "esavEsac", 1);
		detachedCriteria.setFetchMode(Empsalaryacctitems.PROP_ESAI_ESDD,
				FetchMode.JOIN);
		detachedCriteria.add(Restrictions.in("esaiEsav.id", acctversionIds));
		detachedCriteria.addOrder(Order
				.asc(Empsalaryacctitems.PROP_ESAI_DATA_SEQ));
		List acctItems = empsalaryacctitemsDao.findByCriteria(detachedCriteria);
		return acctItems;
	}

	public Map getAcctItemsByPay(List payList) {
		if (payList == null || payList.size() == 0)
			return null;
		Set strBuffer = new TreeSet();
		String key;
		for (Iterator i$ = payList.iterator(); i$.hasNext(); strBuffer.add(key)) {
			Empsalarypay item = (Empsalarypay) i$.next();
			key = item.getEspEsavId().getId();
		}

		List acctversionIds = new ArrayList();
		acctversionIds.addAll(strBuffer);
		if (acctversionIds.size() > 0)
			return getItemsByAcctversion(acctversionIds);
		else
			return null;
	}

	public Map getItemsByEmpbenefitplan(List ebpList) {
		if (ebpList == null || ebpList.size() == 0)
			return null;
		Set strBuffer = new TreeSet();
		String key;
		for (Iterator i$ = ebpList.iterator(); i$.hasNext(); strBuffer.add(key)) {
			Empbenefitplan item = (Empbenefitplan) i$.next();
			key = item.getEbpEsavId().getId();
		}

		List acctversionIds = new ArrayList();
		acctversionIds.addAll(strBuffer);
		if (acctversionIds.size() > 0)
			return getItemsByAcctversion(acctversionIds);
		else
			return null;
	}

	public Map getItemsBySalaryConf(List salaryConfList) {
		Set strBuffer = new TreeSet();
		String key;
		for (Iterator i$ = salaryConfList.iterator(); i$.hasNext(); strBuffer
				.add(key)) {
			Empsalaryconfig item = (Empsalaryconfig) i$.next();
			key = item.getEscEsavId().getId();
		}

		List acctversionIds = new ArrayList();
		acctversionIds.addAll(strBuffer);
		if (acctversionIds.size() > 0)
			return getItemsByAcctversion(acctversionIds);
		else
			return null;
	}

	/**
	 * @deprecated Method getItemsByEmployeeConf is deprecated
	 */

	public Map getItemsByEmployeeConf(List EmpList) {
		Set strBuffer = new TreeSet();
		Iterator i$ = EmpList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Employee item = (Employee) i$.next();
			if (item.getConfig() != null) {
				String key = item.getConfig().getEscEsavId().getId();
				strBuffer.add(key);
			}
		} while (true);
		List acctversionIds = new ArrayList();
		acctversionIds.addAll(strBuffer);
		if (acctversionIds.size() > 0)
			return getItemsByAcctversion(acctversionIds);
		else
			return null;
	}

	/**
	 * @deprecated Method setAcctItemsConfig is deprecated
	 */

	public boolean setAcctItemsConfig(List empList) {
		Set strBuffer = new TreeSet();
		boolean result = true;
		for (Iterator i$ = empList.iterator(); i$.hasNext();) {
			Employee item = (Employee) i$.next();
			if (item.getConfig() == null) {
				result = false;
			} else {
				String key = item.getConfig().getEscEsavId().getId();
				strBuffer.add(key);
			}
		}

		if (strBuffer.size() == 0)
			return false;
		List acctversionIds = new ArrayList();
		acctversionIds.addAll(strBuffer);
		Map acctItemsMap = getItemsByAcctversion(acctversionIds);
		Iterator i$ = empList.iterator();
		do {
			if (!i$.hasNext())
				break;
			Employee item = (Employee) i$.next();
			Empsalaryconfig config = item.getConfig();
			if (config != null)
				config.setAcctItems((List) acctItemsMap.get(config
						.getEscEsavId().getId()));
		} while (true);
		return result;
	}

	/**
	 * @deprecated Method setAcctItemsPay is deprecated
	 */

	public boolean setAcctItemsPay(List payList) {
		if (payList == null || payList.size() == 0)
			return false;
		Map acctItemsMap = getAcctItemsByPay(payList);
		Empsalarypay pay;
		for (Iterator i$ = payList.iterator(); i$.hasNext(); pay
				.setAcctItems((List) acctItemsMap.get(pay.getEspEsavId()
						.getId())))
			pay = (Empsalarypay) i$.next();

		return true;
	}

	public boolean setAcctItemsPay(Empsalarypay payList[]) {
		if (ObjectUtils.isEmpty(payList))
			return false;
		Map acctItemsMap = getAcctItemsByPay(Arrays.asList(payList));
		if (acctItemsMap == null || acctItemsMap.size() == 0)
			return false;
		Empsalarypay arr$[] = payList;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			Empsalarypay pay = arr$[i$];
			pay.setAcctItems((List) acctItemsMap
					.get(pay.getEspEsavId().getId()));
		}

		return true;
	}

	public boolean setAcctItemsEbp(Empbenefitplan ebpList[]) {
		if (ObjectUtils.isEmpty(ebpList))
			return false;
		Map acctItemsMap = getItemsByEmpbenefitplan(Arrays.asList(ebpList));
		if (acctItemsMap == null || acctItemsMap.size() == 0)
			return false;
		Empbenefitplan arr$[] = ebpList;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			Empbenefitplan ebp = arr$[i$];
			ebp.setAcctItems((List) acctItemsMap
					.get(ebp.getEbpEsavId().getId()));
		}

		return true;
	}

	public boolean setAcctItemsConfig(Empsalaryconfig configList[]) {
		if (ObjectUtils.isEmpty(configList))
			return false;
		Map acctItemsMap = getItemsBySalaryConf(Arrays.asList(configList));
		if (acctItemsMap == null || acctItemsMap.size() == 0)
			return false;
		Empsalaryconfig arr$[] = configList;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			Empsalaryconfig config = arr$[i$];
			config.setAcctItems((List) acctItemsMap.get(config.getEscEsavId()
					.getId()));
		}

		return true;
	}

	public Map getItemsBySalaryAdj(List salaryAdjs) {
		Set strBuffer = new TreeSet();
		String key;
		for (Iterator i$ = salaryAdjs.iterator(); i$.hasNext(); strBuffer
				.add(key)) {
			Empsalaryadj item = (Empsalaryadj) i$.next();
			key = item.getEsaEsavIdCur().getId();
			strBuffer.add(key);
			key = item.getEsaEsavIdPro().getId();
		}

		List acctversionIds = new ArrayList();
		acctversionIds.addAll(strBuffer);
		if (acctversionIds.size() > 0)
			return getItemsByAcctversion(acctversionIds);
		else
			return null;
	}

	public List findAll() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empsalaryacctitems.class);
		detachedCriteria.addOrder(Order
				.asc(Empsalaryacctitems.PROP_ESAI_DATA_SEQ));
		return empsalaryacctitemsDao.findByCriteria(detachedCriteria);
	}

	public Boolean deleteEmpsalaryacctitems(String esavId) {
		try {
			String hql = (new StringBuilder())
					.append(
							"delete Empsalaryacctitems as esai where esai.esaiEsav.id='")
					.append(esavId).append("'").toString();
			empsalaryacctitemsDao.exeHql(hql);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Boolean.valueOf(false);
	}

	public Boolean insertEmpsalaryacctitems(
			Empsalaryacctitems empsalaryacctitems) {
		try {
			empsalaryacctitemsDao.saveObject(empsalaryacctitems);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			return Boolean.valueOf(false);
		}
	}

	public boolean saveOrupdate(Collection objs) {
		try {
			empsalaryacctitemsDao.saveOrupdate(objs);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public Empsalaryacctitems loadObject(String esaID, String fetch[]) {
		String hql = (new StringBuilder()).append(
				"from Empsalaryacctitems as esai where id='").append(esaID)
				.append("'").toString();
		return (Empsalaryacctitems) empsalaryacctitemsDao
				.loadObjectByEsaId(hql);
	}

	public List exeHqlList(String hql) {
		return empsalaryacctitemsDao.exeHqlList(hql);
	}

	public List getObjects(Class clas, String fetch[]) {
		return empsalaryacctitemsDao.getObjects(clas, fetch);
	}

	public IEmpSalaryAcctitemsDao getEmpsalaryacctitemsDao() {
		return empsalaryacctitemsDao;
	}

	public void setEmpsalaryacctitemsDao(
			IEmpSalaryAcctitemsDao empsalaryacctitemsDao) {
		this.empsalaryacctitemsDao = empsalaryacctitemsDao;
	}

	private IEmpSalaryAcctitemsDao empsalaryacctitemsDao;
}
